import projectSetting from '@/settings/projectSetting';
import { defineStore } from 'pinia';
import pinia from '../';
import { deepMerge } from '@vc/utils';
import type {
  MultiTabsSetting,
  ProjectConfig,
  TransitionSetting
} from '@/settings/types';

interface AppState {
  projectConfig: ProjectConfig;
}

export const useAppStore = defineStore({
  id: 'app',
  state: (): AppState => ({
    projectConfig: projectSetting,
  }),
  getters: {
    getProjectConfig (): ProjectConfig {
      return this.projectConfig || ({} as ProjectConfig);
    },
    getTransitionSetting (): TransitionSetting {
      return this.getProjectConfig.transitionSetting;
    },
    /** 获取多 tab 的配置 */
    getMultiTabsSetting (): MultiTabsSetting {
      return this.getProjectConfig.multiTabsSetting;
    }
  },
  actions: {
    setProjectConfig (config: DeepPartial<ProjectConfig>): void {
      this.projectConfig = deepMerge(this.projectConfig || {}, config);
    }
  }
});

// Need to be used outside the setup
export function useAppStoreWithOut () {
  return useAppStore(pinia);
}
